package io.renren.modules.dangan.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mchange.v1.util.CollectionUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.dangan.entity.TNzyxmEntity;
import io.renren.modules.dangan.service.TNzyxmService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.dangan.entity.TNzydkwktEntity;
import io.renren.modules.dangan.service.TNzydkwktService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * ${comments}
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-12-08 19:00:47
 */
@RestController
@RequestMapping("tnzydkwkt")
@CrossOrigin
public class TNzydkwktController {
    @Autowired
    private TNzydkwktService tNzydkwktService;
    @Autowired
    private TNzyxmService tNzyxmService;

    @RequestMapping("/getByXmGuid")
    public R getByXmGuid(String xmGuid){
        List<TNzyxmEntity> tNzyxmEntities=tNzyxmService.list(new QueryWrapper<TNzyxmEntity>().eq("bpxm_guid",xmGuid));
        if(CollectionUtil.isEmpty(tNzyxmEntities)){
            return R.ok().put("tnzydkwktList",new ArrayList<>());
        }
        List<String> xmguidList=tNzyxmEntities.stream().map(TNzyxmEntity::getXmGuid).collect(Collectors.toList());
        List<TNzydkwktEntity> tNzydkwktEntityList=
                tNzydkwktService.list(new QueryWrapper<TNzydkwktEntity>().in("xm_guid",xmguidList));

        return R.ok().put("tnzydkwktList",tNzydkwktEntityList);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dangan:tnzydkwkt:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tNzydkwktService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{dkGuid}")
    @RequiresPermissions("dangan:tnzydkwkt:info")
    public R info(@PathVariable("dkGuid") String dkGuid){
        TNzydkwktEntity tNzydkwkt = tNzydkwktService.getById(dkGuid);

        return R.ok().put("tNzydkwkt", tNzydkwkt);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dangan:tnzydkwkt:save")
    public R save(@RequestBody TNzydkwktEntity tNzydkwkt){
        tNzydkwktService.save(tNzydkwkt);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dangan:tnzydkwkt:update")
    public R update(@RequestBody TNzydkwktEntity tNzydkwkt){
        ValidatorUtils.validateEntity(tNzydkwkt);
        tNzydkwktService.updateById(tNzydkwkt);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dangan:tnzydkwkt:delete")
    public R delete(@RequestBody String[] dkGuids){
        tNzydkwktService.removeByIds(Arrays.asList(dkGuids));

        return R.ok();
    }

}
